package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Query;
import io.renren.modules.store.entity.TStoreSellerEntity;
import io.renren.modules.store.service.TStoreSellerService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-21 13:03:43
 */
@RestController
@RequestMapping("store/tstoreseller")
public class TStoreSellerController extends AbstractController {
    @Autowired
    private TStoreSellerService tStoreSellerService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoreseller:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tStoreSellerService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 审核申请
     */
    @RequestMapping("/checkSeller/{id}/{status}")
    public R checkSeller(@PathVariable("id") Long id,@PathVariable("status") int status){
        UpdateWrapper<TStoreSellerEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",status);
        updateWrapper.eq("id",id);
        tStoreSellerService.update(updateWrapper);
        return R.ok();
    }

    /**
     * 根据用户id查询它用的餐馆
     */
    @RequestMapping("/listByUserId")
    public R listByUserId(){
        SysUserEntity userEntity = sysUserService.getById(getUserId());
        QueryWrapper<TStoreSellerEntity> queryWrapper = new QueryWrapper<TStoreSellerEntity>();
        queryWrapper.eq("user_id",getUserId()).or().eq("seller_id",userEntity.getSellerId());
        List<TStoreSellerEntity> list = tStoreSellerService.list(queryWrapper);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("list",list);
        hashMap.put("role",getUserId());
        return R.ok(hashMap);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstoreseller:info")
    public R info(@PathVariable("id") Integer id){
		TStoreSellerEntity tStoreSeller = tStoreSellerService.getById(id);
        return R.ok().put("tStoreSeller", tStoreSeller);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    public R save(@RequestBody TStoreSellerEntity tStoreSeller){
        tStoreSeller.setUserId(getUserId());
        tStoreSellerService.save(tStoreSeller);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoreseller:update")
    public R update(@RequestBody TStoreSellerEntity tStoreSeller){
		tStoreSellerService.updateById(tStoreSeller);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoreseller:delete")
    public R delete(@RequestBody Integer[] ids){
		tStoreSellerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
